import java.util.Arrays;
import java.util.Stack;

public class project {
    public int[] canSeePersonsCount(int[] heights) {
        Stack<Integer> stack=new Stack<>();
        int len=heights.length;
        int[] ret=new int[len];
        for(int i=len-1;i>=0;i--){
            if(i==len-1){
                ret[i]=0;
            }else{
                if(heights[i]<stack.peek()){
                    ret[i]=1;
                }else{
                    int sum=0;
                    while (!stack.isEmpty()&&heights[i]>stack.peek()){
                        stack.pop();
                        sum++;
                    }
                    ret[i]=stack.isEmpty()?sum:sum+1;
                }
            }
            stack.add(heights[i]);
        }
        return ret;
    }


















    public int minNumberOperations(int[] target) {
        int ret=0;
        for(int i=0;i<target.length;i++){
            if(i==0){
                ret+=target[i];
            }else{
                if(target[i]>target[i-1]){
                    ret+=target[i]-target[i-1];
                }
            }
        }
        return ret;
    }


















    public int trap(int[] height) {
        int sum=0;
        for(int i=0;i<height.length;i++){
            sum+=height[i];
        }
        int left=0;
        int right=height.length-1;
        int index=1;
        int measure=0;
        while (left<=right){
            while (left<=right&&height[left]<index){
                left++;
            }
            while (left<=right&&height[right]<index){
                right--;
            }
            measure+=right-left+1;
            index++;
        }
        return sum-measure;
    }



















    public int giveGem(int[] gem, int[][] operations) {
        for(int i=0;i<operations.length;i++){
            int x=operations[i][0];
            int y=operations[i][1];
            int tmp=gem[x]/2;
            gem[x]-=tmp;
            gem[y]+=tmp;
        }
        Arrays.sort(gem);
        return gem[gem.length-1]-gem[0];
    }
















    public int[] shortestToChar(String s, char c) {
        int len=s.length();
        int pos=-len;
        int[] arr=new int[len];
        for(int i=0;i<len;i++){
            if(s.charAt(i)==c){
                pos=i;
            }
            arr[i]=i-pos;
        }
        for(int i=pos-1;i>=0;i--){
            if(s.charAt(i)==c){
                pos=i;
            }
            arr[i]=Math.min(arr[i],pos-i);
        }
        return arr;
    }
}
